package com.ruoyi.accounting.service.impl;

import com.ruoyi.accounting.domain.AccClassification;
import com.ruoyi.accounting.mapper.AccClassificationMapper;
import com.ruoyi.accounting.service.AccClassificationService;
import com.ruoyi.common.core.enums.DelFlag;
import com.ruoyi.common.core.exception.ServiceException;
import com.ruoyi.common.core.utils.DateUtils;
import com.ruoyi.common.security.utils.SecurityUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@Slf4j
public class AccClassificationServiceImpl implements AccClassificationService {
    @Autowired
    private AccClassificationMapper accClassificationMapper;

    /**
     * 查询分类
     *
     * @param id 分类主键
     * @return 分类
     */
    @Override
    public AccClassification selectAccClassificationById(Long id) {
        return accClassificationMapper.selectAccClassificationById(id);
    }

    /**
     * 查询分类列表
     *
     * @param accClassification 分类
     * @return 分类
     */
    @Override
    public List<AccClassification> selectAccClassificationList(AccClassification accClassification) {
        return accClassificationMapper.selectAccClassificationList(accClassification);
    }

    /**
     * 新增分类
     *
     * @param accClassification 分类
     * @return 结果
     */
    @Override
    public int insertAccClassification(AccClassification accClassification) {
        checkDuplicateName(accClassification);
        accClassification.setCreateTime(DateUtils.getNowDate());
        accClassification.setCreateBy(SecurityUtils.getUsername());
        accClassification.setDelFlag(DelFlag.normal.getCode());
        return accClassificationMapper.insertAccClassification(accClassification);
    }

    /**
     * 修改分类
     *
     * @param accClassification 分类
     * @return 结果
     */
    @Override
    public int updateAccClassification(AccClassification accClassification) {
        accClassification.setUpdateTime(DateUtils.getNowDate());
        return accClassificationMapper.updateAccClassification(accClassification);
    }

    /**
     * 批量删除分类
     *
     * @param ids 需要删除的分类主键
     * @return 结果
     */
    @Override
    public int deleteAccClassificationByIds(Long[] ids) {
        return accClassificationMapper.deleteAccClassificationByIds(ids);
    }

    /**
     * 删除分类信息
     *
     * @param id 分类主键
     * @return 结果
     */
    @Override
    public int deleteAccClassificationById(Long id) {
        return accClassificationMapper.deleteAccClassificationById(id);
    }

    @Override
    public List<AccClassification> selectLevelClassificationList(Long bookId) {
        if (bookId == null) {
            throw new ServiceException("参数错误");
        }

        return accClassificationMapper.selectLevelClassificationList(bookId);
    }

    private void checkDuplicateName(AccClassification accClassification) {
        AccClassification where = new AccClassification();
        where.setDelFlag(DelFlag.normal.getCode());
        where.setClassificationName(accClassification.getClassificationName());
        where.setBookId(accClassification.getBookId());
        where.setClassificationType(accClassification.getClassificationType());
        List<AccClassification> accClassifications = selectAccClassificationList(where);
        if (CollectionUtils.isNotEmpty(accClassifications)) {
            throw new ServiceException("分类名称重复");
        }
    }
}
